package org.frxm.utilslibrary;

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

/**
 * 作者 frxm
 * 时间 2017/1/4
 * 版本 1
 * AES:高级加密
 * 对称加密
 *一个密钥，具有加密和解密，要求密钥必须一致
 */

public class AES {
    /*初始化Key密钥*/
    public static Key iniKey(){
        SecureRandom random=new SecureRandom();
        try {
            KeyGenerator kg=KeyGenerator.getInstance("AES");
            kg.init(random);
            return kg.generateKey();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /*加密-AES*/
    public static byte[] encryptAES(Key key,byte[] data)
    {
        try {
            Cipher cipher=Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE,key);
            return cipher.doFinal(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /*解密-DES*/
    public static byte[] decodeAES(Key key,byte[] data){
        try {
            Cipher cipher=Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE,key);
            return cipher.doFinal(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
